package com.hxy.ssm.controller;

import com.hxy.ssm.bean.Dept;
import com.hxy.ssm.service.DeptService;
import com.hxy.ssm.service.impl.DeptServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class DeptController {

    @Autowired
    DeptService deptService;

    @GetMapping("/dept/list")
    public String list(Model model){
        List<Dept> allDept=deptService.findAllDept();
        model.addAttribute("list",allDept);
        return "dept/list";
    }
    @GetMapping("/dept/insertUI")
    public String insertUI()
    {
        return "dept/insert";
    }
    @PostMapping("/dept/insert")
    public String insert(Dept dept){
        deptService.insert(dept);
        return "redirect:list";
    }
    @GetMapping("/dept/updateUI")
    public String updateUI(Integer id,Model model){
        Dept dept=deptService.findDeptById(id);
        model.addAttribute("dept",dept);
        return "dept/update";
    }
    @PostMapping("/dept/update")
    public String update(Dept dept){
        deptService.updateDeptById(dept);
        return "redirect:list";
    }
    @GetMapping("/dept/del")
    public String del(Integer id){
        deptService.delDept(id);
        return "redirect:list";
    }
    @GetMapping("/dept/getList")
    @ResponseBody
    public List<Dept> getList(){
        return deptService.findAllDept();
    }
}
